//
//  hotcalltable.m
//  企业信息查询
//
//  Created by 梁中豪 on 16/5/6.
//  Copyright © 2016年 梁中豪. All rights reserved.
//

#import "hotcalltable.h"
#import <BmobSDK/Bmob.h>
#import "News.h"
#import "CustomCell.h"

@interface hotcalltable ()<UISearchBarDelegate>{
    
    NSMutableArray *_hotcallinfoMutableArray;
}

@property (weak, nonatomic) IBOutlet UISearchBar *hotcallsearch;

@end

@implementation hotcalltable

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置搜索栏委托对象为当前视图控制器
    self.hotcallsearch.delegate = self;
    _hotcallinfoMutableArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    //初次进入查询所有数据
    [self hotcallfilterContentForSearchText:@""];
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点击searchBar旁边的Cancel按钮  退出键盘
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [_hotcallsearch resignFirstResponder];
}


#pragma mark Content Filtering
- (void)hotcallfilterContentForSearchText:(NSString*)searchText;
{
    
    if(!searchText||searchText.length<=0)
    {
        [_hotcallinfoMutableArray removeAllObjects];
        
        BmobQuery *query = [BmobQuery queryWithClassName:@"hotcall"];
        query.limit = 20;
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            for (BmobObject *obj in array) {
                News *info    = [[News alloc] init];
                if ([obj objectForKey:@"Ename"]) {
                    info.Ename = [obj objectForKey:@"Ename"];
                }
                if ([obj objectForKey:@"city"]) {
                    info.city = [obj objectForKey:@"city"];
                }
                if ([obj objectForKey:@"call"]) {
                    info.call = [obj objectForKey:@"call"];
                }
                if ([obj objectForKey:@"adress"]) {
                    info.adress = [obj objectForKey:@"adress"];
                }
                
                [_hotcallinfoMutableArray addObject:info];
            }
            
            [self.tableView reloadData];
        }];
        
        
    }
    else{
        [_hotcallinfoMutableArray removeAllObjects];
        
        BmobQuery *query = [BmobQuery queryWithClassName:@"hotcall"];
        //[query whereKey:@"city" matchesWithRegex:searchText ];
        NSArray *array =  @[@{@"city":@{@"$regex":searchText}},@{@"adress":@{@"$regex":searchText}},@{@"Ename":@{@"$regex":searchText}}];
        [query addTheConstraintByOrOperationWithArray:array];
        
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            for (BmobObject *obj in array) {
                News *info    = [[News alloc] init];
                if ([obj objectForKey:@"Ename"]) {
                    info.Ename = [obj objectForKey:@"Ename"];
                }
                if ([obj objectForKey:@"city"]) {
                    info.city = [obj objectForKey:@"city"];
                }
                if ([obj objectForKey:@"call"]) {
                    info.call = [obj objectForKey:@"call"];
                }
                if ([obj objectForKey:@"adress"]) {
                    info.adress = [obj objectForKey:@"adress"];
                }
                
                
                [_hotcallinfoMutableArray addObject:info];
            }
            
            [self.tableView reloadData];
        }];
        
    }
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_hotcallinfoMutableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"hotcallcell";
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    News *info = (News*)[_hotcallinfoMutableArray objectAtIndex:indexPath.row];
    cell.enpcalllable.text= info.Ename;
    cell.enphotcalllable.text = info.call;
    
    return cell;
}

#pragma mark --UISearchBarDelegate 协议方法
//  获得焦点，成为第一响应者
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.hotcallsearch sizeToFit];
    return YES;
}
//点击键盘上的搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.hotcallsearch.showsScopeBar = NO;
    [self.hotcallsearch sizeToFit];
    [self.hotcallsearch resignFirstResponder];
}
//当文本内容发生改变时候调用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self hotcallfilterContentForSearchText:self.hotcallsearch.text ];
    [self.tableView reloadData];
}


- (IBAction)cancel:(id)sender {
     [self dismissViewControllerAnimated:YES completion:nil];
}

// 选中单元格的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    News *info = (News*)[_hotcallinfoMutableArray objectAtIndex:selectedIndex];
    NSLog(@"didSelect,%@",info.call);
    
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",info.call];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    /*
    UIAlertController *call = [UIAlertController
                                         alertControllerWithTitle:@"拨打电话"
                                         message:@""
                                         preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                        style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction *action){
                                                          
                                    
                                          
                                                      }];
    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"取消"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action){
                                                     }];
    
    [call addAction:yesAction];
    [call addAction:noAction];
    
    //显示
    [self presentViewController:call animated:true completion:nil];
*/
    
}
@end
